//
//  LCDeviceListVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/15.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCDeviceListVC.h"
#import "LCDeviceListCell.h"
#import "LCUsesModel.h"
#import "LCUserModel.h"
#import "LCDeviceModel.h"
#import "LCDeviceDetailInfoVC.h"

@interface LCDeviceListVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@property (weak, nonatomic) IBOutlet UICollectionView *deviceList;

@property (nonatomic, strong) NSArray <LCDeviceModel *>*devicesData;

@property (nonatomic, strong) LCDeviceModel *selectedModel;

@end

@implementation LCDeviceListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.deviceList.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self lc_loadDeviceListData];

    }];
    [self.deviceList.mj_header beginRefreshing];
}

#pragma mark - - UICollectionViewDelegate UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = [indexPath row];
    
    LCDeviceListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"deviceListCell" forIndexPath:indexPath];
    if (self.devicesData.count > row) {
        [cell lc_updateDeviceListWithParameter:self.devicesData[row]];

    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.devicesData.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SWidth, 44);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCLog(@"设备列表 点击 = %ld",(long)indexPath.row);
//    [self bindDevice];
    NSInteger row = [indexPath row];
    self.selectedModel = self.devicesData[row];

    [self performSegueWithIdentifier:@"showDeviceDetailInfo" sender:self];
    
}

#pragma mark - -
//  获取设备列表
- (void)lc_loadDeviceListData {
    
    
    WS(weakSelf);
    [[LCNetwork shareInstance]lc_getNetworkWithURL:@"/api/v0/device/bound" responseSuccess:^(id resultCode, id resultParameter) {
        
        [weakSelf.deviceList.mj_header endRefreshing];

        NSString *resultCodeStr = [NSString stringWithFormat:@"%@",resultCode];
        if ([resultCodeStr isEqualToString:@"0"]) {
            LCLog(@"resultParameter = %@",resultParameter);
            
            NSArray *data = resultParameter[@"data"];
            if (data.count > 0) {
                NSMutableArray *devices = [LCDeviceModel mj_objectArrayWithKeyValuesArray:data];
                self.devicesData = devices;
                [weakSelf.deviceList reloadData];
            }

        }
    } responseFail:^(id resultCode, NSError *error) {
        [weakSelf.deviceList.mj_header endRefreshing];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    LCDeviceDetailInfoVC *deviceDetail = [segue destinationViewController];
    deviceDetail.deviceModel = self.selectedModel;
}


@end
